package com.eight.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.AclPermission;
import com.eight.common.pojo.AclUser;
import com.eight.common.vo.Result;
import com.eight.system.service.PermissionService;
import com.eight.system.vo.PermissionVo;
import com.eight.system.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.acl.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/23 21:15
 */
@RestController
@RequestMapping("system/permission")
@Api("菜单管理")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("leftMenu")
    @ApiOperation(value = "获取当前用户具有的左侧菜单")
    public Result leftMenu(HttpServletRequest request){
        //1.从请求头获取token信息
        String token = request.getHeader("token");
        //2.从redis中获取当前登录者的信息
        String json = redisTemplate.opsForValue().get("user::" + token);
        AclUser user = JSON.parseObject(json,AclUser.class);
        List<AclPermission> permissions = permissionService.findByUserId(user.getId());
        return Result.success(permissions);
    }
    @PostMapping("/list/{roleId}")
    @ApiOperation(value = "查询所有权限以及角色具有的权限")
    public Result list(@PathVariable String roleId){
        List<AclPermission> allPermissions= permissionService.findAll();
        List<String> checkItems = permissionService.findByRoleId(roleId);
        Map<String,Object> map=new HashMap<>();
        map.put("allPermissions",allPermissions);
        map.put("checkItems",checkItems);
        return Result.success(map);
    }

    @GetMapping("confirm")
    @ApiOperation("确认分配权限")
    public Result confirm(String roleId,String[] permissionIds){
        permissionService.confirm(roleId,permissionIds);
        return Result.success();
    }
    @PostMapping("/findPage/{currentPage}/{pageSize}")
    @ApiOperation(value = "分页条件查询")
    public Result list(@PathVariable Integer currentPage,
                       @PathVariable Integer pageSize,
                       @RequestBody PermissionVo permissionVo){
        Page<AclPermission> page = permissionService.findPage(currentPage, pageSize, permissionVo);
        return Result.success(page);
    }

    @ApiOperation("添加菜单")
    @PutMapping("/insert")
    public Result addUserByUser(@RequestBody AclPermission aclPermission){
        int insert = permissionService.insert(aclPermission);
        return Result.success();
    }
    @ApiOperation("根据ID删除用户")
    @DeleteMapping("/deleteById/{ids}")
    public Result deleteUserById(@PathVariable List ids){
        int userById = permissionService.deleteById(ids);
        return Result.success();
    }
    @ApiOperation("更新菜单")
    @PostMapping("update")
    public Result update(@RequestBody AclPermission aclPermission){
        int update = permissionService.update(aclPermission);
        return Result.success(update);
    }


}
